#!/usr/bin/env python
# Copyright (c) 2011-2012 Chanrithy Thim
#
# AUTHOR:
# Chanrithy Thim <12rithy@gmail.com>
#
# This file is part of Appshell
#
# Appshell is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation; either version 2 of the License, or (at
# your option) any later version.
#
# Appshell is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with AptUrl; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import os
import os.path
import tempfile
import sys

from Appshell.manifest import *
from Appshell.actions import *
from Appshell.utils import *

def usage():
    print ("Usage: appshell COMMAND\n"
            "\n"
            "Commands:\n"
            "  -i, install <.app>       Install applicatoins\n"
            "  -u, uninstall <appname>  Uninstall applications\n"
            "  -b, build                Build applications from current directory\n"
            "  -h, help                 Show this message")
    
if __name__ == "__main__":
    # Looking for argument
    if len(sys.argv[1:]) == 0:
        usage()
        sys.exit(0)
    if ("-h" in sys.argv[1] or "--help" in sys.argv[1]):
        usage()
        sys.exit(0)
    elif ("-i" in sys.argv[1] or "install" in sys.argv[1]):
        if len(sys.argv[1:]) != 1:
            if sys.argv[3:]:
                print ("ERROR: appshell %s takes exactly 1 argument (%s given)" % (sys.argv[1], len(sys.argv[2:]))
                usage()
                sys.exit(1)
            else:
                # Check for valid appimage file
                fpath = os.path.realpath(path)
                if not is_appimage(fpath):
                    print "Invalid file"
                    sys.exit(1)
                # Create temporary folder and mount appimage
                mtmp = tempfile.mkdtemp('.appshell')
                mount(fpath, mtmp)
                
                # Parse manifest and check installed version
                manifest = Manifest(mtmp + "/manifest.xml")
                if check_installed(manifest.name):
                    print "%s already installed" % manifest.name
                    umount(mtmp)
                    sys.exit(0)
                
                # Checking disk space
                root_size = utils.partition_freespace("/")
                app_size = utils.dir_size(fpath)
                if int_size * 2 > root_size:
                    print "ERROR: Not enough disk space to continue..."
                    print "Free disk space must be x2 of applications size"
                    sys.exit(1)
                        
                # Install application
                print "Installing..."
                install(manifest.name, fpath, mtmp)
                print "Finished"
        else:
            print "ERROR: appshell %s takes exactly 1 argument (%s given)" % (sys.argv[1], len(sys.argv[2:]))
            usage()
            sys.exit(1)
    elif ("-u" in sys.argv[1] or "uninstall" in sys.argv[1]):
        # Check installed
        if check_installed(sys.argv[1]):
            uninstall(sys.argv[1], "/Apps/" + sys.argv[1] + ".app")
    elif ("-b" in sys.argv[1] or "build" in sys.argv[1]):
        build()
    else:
        usage()
        sys.exit(0)
        
